package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.dao.FeedbackDAO;
import cj.web.admin.dao.FeedbackReplyDAO;
import cj.web.admin.domain.Feedback;
import cj.web.constants.FeedbackEnum;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

/**
 * 回答
 * @author Administrator
 *
 */
@Path("/feedback")
public class FeedbackController extends BaseController{

	private static Logger logger = Logger.getLogger(FeedbackController.class.getName());
	
	@Autowired
	private FeedbackDAO feedbackDao;
	@Autowired
	private FeedbackReplyDAO feedbackReplyDAO;
	
	@Get("show")
	public String show(Model model,Feedback feedback) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdyplgl);
        model.add("FeedbackEnum",EnumSet.allOf(FeedbackEnum.class));
		return "/views/news/feedbackList.vm";
	}

	@Post("getList")
	public String queryList(Feedback feedback) {
	
		List<Feedback> arr = feedbackDao.query(feedback);
		
		for(Feedback obj:arr)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
//			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = feedbackDao.querySize(feedback);

		jo.put("data", arr);
		jo.put("draw", feedback.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Feedback obj = feedbackDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdyplgl);
		return "/views/news/feedbackEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Feedback feedback,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			feedback.setStatus(FeedbackEnum.YGB.getValue());
			feedback.setUpdateUser(returnSessionObj(request).getAccount());
			feedbackDao.update(feedback);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		Feedback obj = feedbackDao.queryById(id);
		
		if(obj!=null)
		{
			feedbackDao.deleteById(id);
		}
		return "r:show";
	}
	
	//置顶
	@Get("stick")
    public String stick(@Param("id") int id) {
		
		Feedback obj = feedbackDao.queryById(id);
		
		if(obj!=null)
		{
			feedbackDao.updateByStickTime(id);
		}
		
		return "r:show";
	}
	
	//取消置顶
	@Get("channelStick")
    public String channelStick(@Param("id") int id) {
		
		Feedback obj = feedbackDao.queryById(id);
		
		if(obj!=null)
		{
			feedbackDao.updateByStickTimeIsNull(id);
		}
		
		return "r:show";
	}
}